//
//  SpeedPurchaseViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "SpeedPurchaseViewController.h"
#import "SpeedPurViewModel.h"
#import "ProductNameViewController.h"
#import "ConfirmPurchaseViewController.h"
#import "PurchareModel.h"

@interface SpeedPurchaseViewController ()
@property (nonatomic,strong)SpeedPurViewModel * speedViewModel;
@end

@implementation SpeedPurchaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发布求购";
    self.speedViewModel = [[SpeedPurViewModel alloc] initWithViewToViewModel:self.tableView thePurchaseType:_p_type theSuperView:self.view];
    RACSubject *subject = [RACSubject subject];
    [subject subscribeNext:^(id  _Nullable x) {
        CardInforModel * s_cardModel = (CardInforModel *)x;
        ProductNameViewController * p_nameVc = [ProductNameViewController new];
        p_nameVc.publish_Type = s_cardModel.rowNum;
        p_nameVc.Block_PubModel = ^(PublishListModel_Data * _Nonnull p_dataModel) {
            switch (s_cardModel.rowNum) {
                case 0:{
                    s_cardModel.productId = p_dataModel.productId;
                    s_cardModel.productName = p_dataModel.productName;
                }
                    break;
                case 1:{
                    s_cardModel.materialId = p_dataModel.materialId;
                    s_cardModel.materialName = p_dataModel.materialName;
                }
                    break;
                case 2:{
                    s_cardModel.specificationsId = p_dataModel.spec_id;
                    s_cardModel.specificationsName = p_dataModel.specifications;
                }
                    break;
                case 3:{
                    s_cardModel.factoryName = p_dataModel.factoryName;
                }
                    break;
                    
                default:
                    break;
            }
            [self.speedViewModel.headerView relodCollection];
        };
        [self.navigationController pushViewController:p_nameVc animated:YES];
    }];
    self.speedViewModel.subject = subject;
    //
    kWeakSelf(self);
    self.speedViewModel.Block_SubmitAction = ^{
        if (weakself.speedViewModel.purInforModel.quoteLimit == 0) {
            [GLGWProgressHUD showNotice:@"请选择报价截止时间！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.payType == 0) {
            [GLGWProgressHUD showNotice:@"请选择支付方式！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.logisticsRequire == 0) {
            [GLGWProgressHUD showNotice:@"请选择物流要求！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.companyName.length == 0) {
            [GLGWProgressHUD showNotice:@"请填写买家公司名称！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.contacts.length == 0) {
            [GLGWProgressHUD showNotice:@"请填写联系人！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.contactPhone.length == 0) {
            [GLGWProgressHUD showNotice:@"请填写联系人手机号！" view:nil];
            return;
        }
        if (weakself.speedViewModel.purInforModel.privacySetting == 0) {
            [GLGWProgressHUD showNotice:@"请选择隐私设置！" view:nil];
            return;
        }
        BOOL goBool = NO;
        for (CardInforModel * cardModel in weakself.speedViewModel.headerView.dataSourceArr) {
            if (cardModel.productName.length == 0) {
                goBool = YES;
            }
            if (cardModel.materialName.length == 0) {
                goBool = YES;
            }
            if (cardModel.specificationsName.length == 0) {
                goBool = YES;
            }
            if (cardModel.factoryName.length == 0) {
                goBool = YES;
            }
            if (cardModel.weight == 0) {
                goBool = YES;
            }
        }
        if (goBool) {
            [GLGWProgressHUD showNotice:@"请补全商品信息！" view:nil];
            return;
        }
        weakself.speedViewModel.purInforModel.quoteLimit =-1;
        weakself.speedViewModel.purInforModel.goodsArr = weakself.speedViewModel.headerView.dataSourceArr;
        ConfirmPurchaseViewController * confirmVC = [ConfirmPurchaseViewController new];
        confirmVC.pur_Model = weakself.speedViewModel.purInforModel;
        confirmVC.pur_type = weakself.p_type;//求购类型
        [weakself.navigationController pushViewController:confirmVC animated:YES];
    };
}
#pragma mark --- set
-(void)setP_type:(PurchaseType)p_type{
    _p_type = p_type;
}
@end
